package com.mach.platform.controller;

import com.mach.platform.domain.base.Permission;
import com.mach.platform.domain.Response;
import com.mach.platform.service.PermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Administrator
 * @Description: 权限控制器
 * @Package: com.mach.platform.com.mach.platform.controller
 * @time 2018/4/23 0023 11:56
 */
@Api(tags = { "权限管理接口" })
@RestController
@RequestMapping("$perm")
public class PermissionCtrl extends BaseCtrl<Permission> {

	@Autowired
	private PermissionService permissionService;

	/**
	 * -- @GetMapping(value = "/xxx/{id}")
	 * -- @PathVariable(value = "id", required = false) Long id
	 * 单个数据 的接口
	 *
	 * @param id 数据的id
	 * @return Response
	 */
	@GetMapping(value = "/perm/{id}")
	@Override
	public Response info (@PathVariable(value = "id", required = true) Long id) {
		return Response.ok(permissionService.info(id));
	}

	/**
	 * 获取列表数据
	 * --@GetMapping(value = "/xxxs")
	 * <p>
	 * <p>
	 * -- @PageableDefault(size = 25) Pageable pageable
	 * --  @RequestParam Map params
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Response
	 */
	@ApiOperation(value = "列表--分页", produces = "application/json", notes = "列表参数：size(每页数量),page(页码),searchParam(查询内容)")
	@GetMapping(value = "/list")
	@Override
	public Response list (@PageableDefault(size = 25) Pageable pageable, @RequestParam Map params) {

		Page<Permission> dbResult = permissionService.list(pageable, params);

		return Response.ok(dbResult);
	}

	/**
	 * -- @DeleteMapping("/xxx")
	 * -- @RequestBody Long[] ids
	 * 删除数据
	 *
	 * @param ids id的集合
	 * @return Response
	 */
	@ApiOperation(value = "删除", produces = "application/json")
	@DeleteMapping("/del")
	@Override
	public Response delete (@RequestBody Long[] ids) {

		Integer effections = permissionService.delete(ids);
		if (effections > 0) {
			return Response.ok("成功删除了" + effections + "条数据！");
		} else {
			return Response.error("删除不成功！");
		}

	}

	/**
	 * 保存一个 对象
	 * --@PostMapping("/xxx")
	 * --@RequestBody T t
	 *
	 * @param permission 保存的实体
	 * @return Response
	 */
	@ApiOperation(value = "新增", produces = "application/json")
	@PostMapping("/save")
	@Override
	public Response save (@RequestBody Permission permission) {

		Permission saveObj = permissionService.save(permission);

		return Response.ok(saveObj);
	}

	/**
	 * 更新一个对象
	 * --@PutMapping("/xxx")
	 * --@RequestBody T t
	 *
	 * @param permission 更新的实体
	 * @return Response
	 */
	@ApiOperation(value = "修改保存", produces = "application/json")
	@PutMapping("/upd")
	@Override
	public Response update (@RequestBody Permission permission) {

		Permission updateObj = permissionService.update(permission);

		return Response.ok(updateObj);
	}

	/**
	 * 加载角色----全部-----所关联的权限列表和【没关联的权限列表】
	 *
	 * @param roleId 要查询的角色id
	 * @return Response
	 */
	@ApiOperation(value = "加载角色----全部-----所关联的权限列表和【没关联的权限列表】", produces = "application/json")
	@PostMapping("rolePerms")
	public Response loadRolsPermissionHasOrNot (@ApiParam(value = "要查询的角色id") @RequestParam("roleId") Long roleId) {

		Map dbResult = permissionService.loadRolsPermissionHasOrNot(roleId);

		return Response.ok(dbResult);
	}

}
